package com.jdk.learn.exam.gui;

import org.junit.Test;

import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by ricky
 * on 2018/2/28.
 一维图形
 绘图相关:
    线性
    填充
 椭圆,矩形，多边形等
 */
public class GraphicsDemo {


    /**
     * Color示例
     *  创建Color的方式有两种，一种是根据RGB
     *  一种是根据Color中预设的13种方案
     * */
    @Test
    public void ColorDemo() {

        Color black=new Color(0,0,0);//黑色
        Color black2=Color.black;//黑色2

        JComponent component=new JPanel();
        component.setForeground(Color.white);//前景色为白
        component.setBackground(black);//背景色为黑
    }
    /**
     * 字体相关
     *
     * */
    @Test
    public void FontDemo() {
        //黑体 加粗 14号
        Font first=new Font("黑体",Font.BOLD,14);

        GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] names = env.getAvailableFontFamilyNames();
        for (int i = 0; i <names.length ; i++) {
            System.out.println(names[i]+",");
        }
    }
    /**
     * 绘图相关
     *
     * */
    @Test
    public void GranphicsDemo() {
        Main main=new Main();
        main.go();
        System.out.println("---------");
    }

    public static void main(String[] args) {
        Main main=new Main();
        main.go();
        System.out.println("---------");
    }

}

class Main implements ActionListener {
    JFrame frame = GUIUtils.getDefaultFrame("绘图");
    MyButton button=new MyButton("绘制");
    MyPanel panel=new MyPanel();
    int tag=1;
    public static void main(String[] args) {

    }

    public void go() {
        button.addActionListener(this);
        Container container = frame.getContentPane();
        container.add(button,"South");
        container.add(panel,"Center");
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (tag == 0) {
            tag = 1;
            button.setText("绘制");
        } else {
            tag=0;
            button.setText("清除");
        }
        panel.repaint();
    }


    class MyButton extends JButton {
        public MyButton(String text) {
            super(text);
        }

        @Override
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            int width = getWidth();
            int height = getHeight();
            g.drawOval(4,4,width-8,height-8);//绘制椭圆
        }
    }

    /**
     * 自定义面板
     */
    class MyPanel extends JPanel {
        @Override
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            int x[]={280,300,320,290,260};
            int y[]={120,120,130,150,130};
            if (tag==0) {
                g.setColor(Color.blue);

                g.drawLine(40,25,30,50);//绘制直线

                g.setColor(Color.green);

                g.drawRect(100,50,100,46);//绘制矩形

                g.setColor(Color.red);

                g.drawRoundRect(73,32,56,37,10,16);//绘制圆角矩形

                g.setColor(Color.yellow);
                g.fillOval(180,60,60,45);//填充椭圆

                g.setColor(Color.pink);
                g.fillArc(250,32,90,60,15,30);//填充圆弧

                g.setColor(Color.MAGENTA);
                g.fillPolygon(x,y,5);//填充多边形

                g.setColor(Color.red);
                g.fillRect(10,110,80,30);

                g.setColor(Color.green);
                g.fillRect(50,120,80,30);

                g.setXORMode(Color.blue);//设置为XOR绘图模式
                g.fillOval(90,130,80,30);
            }
        }
    }
}
